/**
 * User: alesj
 * Date: 2004.8.25
 * Time: 9:50:34
 */

package com.generalynx.common.data;

import java.io.Serializable;
import java.util.Properties;

import com.generalynx.common.resources.Resources;
import com.generalynx.ecos.utils.Context;
import com.generalynx.ecos.utils.IBindable;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.validation.DataBinder;
import org.springframework.validation.Validator;

public abstract class ADEData extends DefaultEntity implements IADEData, IBindable, Serializable {

    private static final long serialVersionUID = 1L;

    //add, edit, delete pictures
    private static String KEY_PREFIX = "common.";
    private static String m_editIcon;
    private static String m_deleteIcon;
    private static String m_selectIcon;

    private String label;

    public static void init(Properties properties) {
        m_deleteIcon = properties.getProperty(KEY_PREFIX + "delete_icon");
        m_editIcon = properties.getProperty(KEY_PREFIX + "edit_icon");
        m_selectIcon = properties.getProperty(KEY_PREFIX + "select_icon");
    }

    protected abstract String getNameLabel();

    protected String getInfo() {
        return String.valueOf(getId());
    }

    protected String getSelectAction() {
        return null;
    }

    protected String[] getSelectActions() {
        return null;
    }

    public String getAdeLabel() {
        StringBuffer buf = new StringBuffer();
        buf.append("<tr>\n");
        String name = (label != null ? label : getNameLabel());
        buf.append("<td>" + name + " (" + getInfo() + ") </td>");
        buf.append("<td><a href=\"#\" onclick=\"return submitForm('edit', " + getId() + ");\"><img src=\"" + m_editIcon + "\" alt=\"" + Resources.getString(Resources.EDIT) + "\"></a></td>");
        buf.append("<td><a href=\"#\" onclick=\"return deleteX('delete'," + getId() + ", '" + getNameLabel() + "');\"><img src=\"" + m_deleteIcon + "\" alt=\"" + Resources.getString(Resources.DELETE) + "\"></a></td>");
        String[] actions = getSelectActions();
        if (actions == null) {
            String action = getSelectAction();
            if (action != null) {
                actions = new String[]{action};
            }
        }
        if (actions != null) {
            for (int i = 0; i < actions.length; i++) {
                buf.append("<td><a href=\"" + actions[i] + ".htm?id=" + getId() + "\"><img src=\"" + m_selectIcon + "\" alt=\"" + Resources.getString(Resources.SELECT) + "\"></a></td>");
            }
        }
        buf.append("\n</tr>");
        return buf.toString();
    }

    public void setAdeLabelName(String label) {
        this.label = label;
    }

    protected static boolean entityExists(IEntity entity) {
        return (entity != null && entity.getId() > 0);
    }

    /* override if necessary to instanciate objects before creating or binding */

    public void afterCreate() {
    }

    public void beforeInsert(Context context) {
    }

    public void beforeSaveOrUpdate(BeanFactory beanFactory) {
    }

    public void registerCustomEditors(DataBinder binder) {
    }

    public void beforeBind() {
    }

    public void afterBind() {
    }

    public Validator validator() {
        return null;
    }

    public String getLabel() {
        return getNameLabel();
    }

}
